package net.xdclass.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.request.LockCouponRecordRequest;
import net.xdclass.service.CouponRecordService;
import net.xdclass.service.CouponService;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.CouponRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 二当家小D
 * @since 2021-12-25
 */
@RestController
@RequestMapping("/api/coupon_record/v1")
public class CouponRecordController {
    @Autowired
    private CouponRecordService couponRecordService;

    @ApiOperation("分頁查詢個人优惠劵")
    @GetMapping("page")
    public JsonData page(@ApiParam(value = "當前頁") @RequestParam(value = "page", defaultValue = "1") int page,
                         @ApiParam(value = "每頁顯示多少條") @RequestParam(value = "size", defaultValue = "10") int size) {
        Map<String, Object> pageResult = couponRecordService.page(page, size);

        return JsonData.buildSuccess(pageResult);
    }

    @ApiOperation("根據优惠券id找詳情記錄")
    @GetMapping("detail/{record_id}")
    public JsonData getCouponDetailById(@ApiParam(value = "記錄id") @PathVariable("record_id") int id) {
        CouponRecordVO couponRecordVO = couponRecordService.findById(id);

        return couponRecordVO == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS) : JsonData.buildSuccess(couponRecordVO);
    }

    @ApiOperation("rpc - 鎖定, 优惠券記錄")
    @PostMapping("lock_records")
    public JsonData lockCouponRecords(@ApiParam("鎖定优惠券請求對象") @RequestBody LockCouponRecordRequest recordRequest) {
        JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);

        return jsonData;
    }
}

